package com.yswy.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.obs.services.model.PutObjectResult;
import com.yswy.common.model.ResultModel;
import com.yswy.common.utils.FileSuffix;
import com.yswy.common.utils.HuaWeiYunOBSUtil;
import com.yswy.common.utils.videoTpGif.FfmpegUtil;
import com.yswy.theater.entity.TheaterPlay;
import com.yswy.theater.entity.TheaterPlayGather;
import com.yswy.theater.service.TheaterPlayGatherService;
import com.yswy.theater.service.TheaterPlayService;
import com.yswy.theater.vo.req.AddTheaterPlay;
import com.yswy.theater.vo.req.AddTheaterPlayCsj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
@Api(description = "上传")
public class UploadController {
    @Autowired
    private TheaterPlayService theaterPlayService;
    @Autowired
    private TheaterPlayGatherService theaterPlayGatherService;
    @Autowired
    private HuaWeiYunOBSUtil huaWeiYunOBSUtil;



    @PostMapping("/addTheaterPlay")
    @ApiOperation(value = "添加短剧")
    public ResultModel addTheaterPlay(@RequestBody AddTheaterPlay addTheaterPlay,
                                      HttpServletRequest request) {
        theaterPlayService.addTheaterPlay(addTheaterPlay);
        return ResultModel.ok();
    }

    @PostMapping("/addTheaterPlayByCsj")
    @ApiOperation(value = "添加穿山甲短剧")
    public ResultModel addTheaterPlayByCsj(@RequestBody List<AddTheaterPlayCsj> addTheaterPlayCsjs,
                                           HttpServletRequest request) {
        theaterPlayService.addTheaterPlayByCsj(addTheaterPlayCsjs);
        return ResultModel.ok();
    }

//
//    {
//      "playPath": "黑丝玉足",
//      "playType": 0,
//      "sex": 1,
//      "templateType": 2
//    }
//    {
//      "playPath": "黑丝玉足",
//      "playType": 1,
//      "sex": 1,
//      "templateType": 3
//    }
    @PostMapping("/addPack")
    @ApiOperation(value = "添加午夜剧场素材上传素材")
    public ResultModel addPack(
            @RequestBody() AddTheaterPlay addTheaterPlay,
            HttpServletRequest request) {
        String playName = addTheaterPlay.getPlayPath();
        String playMsg = addTheaterPlay.getPlayPath();

        String playPath = "C:\\Users\\mxy\\Desktop\\"+addTheaterPlay.getPlayPath();
        File file = new File(playPath);
        if (!file.exists()) {
            return ResultModel.error("");
        }
        List<String> fileNames = new ArrayList<>();
        FileSuffix.getFileNames(file, fileNames);

        TheaterPlay theaterPlay = theaterPlayService.getOne(new LambdaQueryWrapper<TheaterPlay>().eq(TheaterPlay::getPlayName , playName));
        if (theaterPlay == null){
            theaterPlay = TheaterPlay.builder()
                    .playName(playName)
                    .playMsg(playMsg)
                    .playType(addTheaterPlay.getPlayType())
                    .isShow(1)
                    .sex(addTheaterPlay.getSex())
                    .episodes(fileNames.size())
                    .build();
            theaterPlayService.save(theaterPlay);
        }
        for (int i = 0; i <fileNames.size()  ; i++) {
            try {
                TheaterPlayGather theaterPlayGather = new TheaterPlayGather();
                theaterPlayGather.setTheaterPlayId(theaterPlay.getId());
                theaterPlayGather.setEpisodes(i+1);
                String s = fileNames.get(i) ;
                String filePath = playPath+"\\"+s ;
                System.out.println(filePath);
                String[] filePaths = s.split("\\.");
                if (addTheaterPlay.getPlayType() == 1){
                    InputStream filePathIn =  new FileInputStream(new File(filePath));
                    PutObjectResult newResult = huaWeiYunOBSUtil.upload( UUID.randomUUID() + "."+filePaths[1] , "play/"+theaterPlay.getId(),filePathIn);
                    theaterPlayGather.setPlayTitleUrl(newResult.getObjectUrl());
                    theaterPlayGatherService.save(theaterPlayGather);
                    if (i == 0){
                        theaterPlay.setPlayTitleUrl(newResult.getObjectUrl());
                        theaterPlayService.updateById(theaterPlay);
                    }
                }else {
                    InputStream filePathIn =  new FileInputStream(new File(filePath));
                    PutObjectResult newResult = huaWeiYunOBSUtil.upload( UUID.randomUUID() + "."+filePaths[1] , "play/"+theaterPlay.getId(),filePathIn);
                    theaterPlayGather.setPlayUrl(newResult.getObjectUrl());

                    String titlePath  = "C:\\Users\\mxy\\Desktop\\temp\\"+UUID.randomUUID() +".jpg" ;
                    FfmpegUtil.videoScreenshot(filePath ,"00:00:00"  ,titlePath);
                    InputStream titlePathIn =  new FileInputStream(new File(titlePath));
                    PutObjectResult titleResult = huaWeiYunOBSUtil.upload( UUID.randomUUID() + ".jpg" , "play/"+theaterPlay.getId(),titlePathIn);
                    theaterPlayGather.setPlayTitleUrl(titleResult.getObjectUrl());
//                    File file1 = new File(titlePath);
//                    file1.delete();
                    theaterPlayGatherService.save(theaterPlayGather);
                    if (i == 0){
                        theaterPlay.setPlayTitleUrl(titleResult.getObjectUrl());
                        theaterPlayService.updateById(theaterPlay);
                    }
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return ResultModel.ok();
    }


}
